package com.tarena.controller;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tarena.entity.Meettingaddress;
import com.tarena.entity.Meettingroom;
import com.tarena.entity.User;
import com.tarena.entity.Viewmodelbooking;
import com.tarena.service.BookingService;
import com.tarena.service.MeettingaddressService;
import com.tarena.service.MeettingroomService;
@Controller
@RequestMapping("/meettingroom")
public class MeettingroomController {
	
	@Resource
	private BookingService bookingService;
	@Resource
	private MeettingroomService  meettingroomService;
	@Resource
	private MeettingaddressService  meettingaddressService;
	private PrintWriter out;
	
	@RequestMapping("/saveMeettingroom.do")
	public void save(Meettingroom meettingroom, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		System.out.println("save");
		boolean save = meettingroomService.save(meettingroom);
		out = resp.getWriter();
		out = resp.getWriter();
		if (save) {
			out.write("1");
			out.flush();
			out.close();
		} else {
			out.write("0");
			out.flush();
			out.close();
		}
	}
	@RequestMapping("/deleteMeettingroom.do")
	public void delete(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		System.out.println("delete");
		boolean delete = meettingroomService.delete(Integer.parseInt(req
				.getParameter("id")));
		out = resp.getWriter();
		if (delete) {
			out.write("1");
			out.flush();
			out.close();
		} else {
			out.write("0");
			out.flush();
			out.close();
		}
	}
	@RequestMapping("/updateMeettingroom.do")
	public void update(Meettingroom meettingroom, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		out = resp.getWriter();
		try {
			meettingroomService.update(meettingroom);
			out.write("1");
			out.flush();
			out.close();
		} catch (Exception e) {
			out.write("0");
			out.flush();
			out.close();
		}
	}
	@RequestMapping("/findMeettingroom")
	public String findAllMeettingroom(Model mode) {
		List<Meettingroom> list = meettingroomService.findAllMeettingroom();
		mode.addAttribute("meettingrooms", list);
		return "meettingroom/meettingroom_list";
	}
	@RequestMapping("/findById.do")
	public String findById(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		List<Meettingroom> list = meettingroomService.findById(Integer
				.parseInt(req.getParameter("id")));
		req.setAttribute("meettingrooms", list);
		return "meettingroom/meettingroom_list";
	}
	
	@RequestMapping("/findByPage.do")
	public void findByPage(Meettingroom meettingroom, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		System.out.println("findByPage");
		String page = req.getParameter("page");
		String rows = req.getParameter("rows");
		System.out.println(page);
		System.out.println(meettingroom.getSeatNum() + "");
		List<Meettingroom> list = null;
		if (null != meettingroom.getSeatNum()) {
			list = meettingroomService.findByPage(Integer.parseInt(rows)
					* (Integer.parseInt(page) - 1), Integer.parseInt(rows),
					meettingroom);
		} else {
			list = meettingroomService.findByPage(Integer.parseInt(rows)
					* (Integer.parseInt(page) - 1), Integer.parseInt(rows));
		}
		System.out.println(list.toString());
		Map<String, Object> jsonMap = new HashMap<String, Object>();// 定义map
		jsonMap.put("total", meettingroomService.getSum());// total键 存放总记录数，必须的
		jsonMap.put("rows", list);// rows键 存放每页记录 list
		JSONObject result = JSONObject.fromObject(jsonMap);// 格式化result
		// 一定要是JSONObject
		out = res.getWriter();
		out.write(result.toString());
		System.out.println(result.toString());
		out.flush();
		out.close();
	}
	
	@RequestMapping("/userfindByPage.do")
	public void userfindByPage( Meettingroom meettingroom, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		List<Meettingroom> list = null;
		List<Viewmodelbooking> returelist = new ArrayList<Viewmodelbooking>();
		
		
		
		String addressId = req.getParameter("meettingAddressId");
		String startTime = req.getParameter("startDate");
		String endTime = req.getParameter("endDate");
		
		
		if (stringnull(addressId)  && stringnull(startTime) && stringnull(endTime) ) {
			List<Meettingaddress> listMA = meettingaddressService.findById(Integer.parseInt(addressId));
			 list = bookingService.giveMeettingroomAll(listMA.get(0),startTime,endTime);
			
		} else {
			String page = req.getParameter("page");
			String rows = req.getParameter("rows");
			System.out.println(page);
			System.out.println(meettingroom.getSeatNum() + "");
			
			if (null != meettingroom.getSeatNum()) {
				list = meettingroomService.findByPage(Integer.parseInt(rows)
						* (Integer.parseInt(page) - 1), Integer.parseInt(rows),
						meettingroom);
			} else {
				list = meettingroomService.findByPage(Integer.parseInt(rows)
						* (Integer.parseInt(page) - 1), Integer.parseInt(rows));
			}
			
			
		}
		
		for(Meettingroom mr : list){
			Viewmodelbooking viewmodelbooking = new Viewmodelbooking();
			Meettingaddress ma = meettingaddressService.findById(mr.getMeettingAddressId()).get(0);
			System.out.println(ma.getId());
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			String format = df.format(new Date());
			
			
			viewmodelbooking.setId(mr.getId());
			viewmodelbooking.setViewuserid(((User)req.getSession().getAttribute("user_online")).getId());
			viewmodelbooking.setViewdata(format);
			viewmodelbooking.setViewstartDate(startTime);
			viewmodelbooking.setViewendDate(endTime);
			viewmodelbooking.setProjector("否");
			viewmodelbooking.setMeettingadress(ma.getAddress());
			viewmodelbooking.setViewphoneEEE(1);
			viewmodelbooking.setViewmeettingrooAddressId(mr.getMeettingAddressId());
			//viewmodelbooking.setViewmeettingroomsddingname();
			if (viewmodelbooking != null) {
				returelist.add(viewmodelbooking);
			}
			
		}
		System.out.println(list.toString());
		Map<String, Object> jsonMap = new HashMap<String, Object>();// 定义map
		jsonMap.put("total", meettingroomService.getSum());// total键 存放总记录数，必须的
		jsonMap.put("rows", returelist);// rows键 存放每页记录 list
		JSONObject result = JSONObject.fromObject(jsonMap);// 格式化result
		// 一定要是JSONObject
		out = res.getWriter();
		out.write(result.toString());
		System.out.println(result.toString());
		out.flush();
		out.close();
	}
	public boolean stringnull(String qqq) {
		if (  qqq == null || "".equals(qqq)) {
			return false;
		}
		return true;
		
		
	}
}
